package com.ust.app.movie.action.login;

import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.ResultPath;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.ust.app.movie.entity.User;
import com.ust.app.movie.service.LoginDao;
//import com.ust.app.movie.service.LoginService;
//import com.ust.app.movie.service.impl.LoginDaoImpl;


@Namespace(value="/")
@ResultPath(value="/")
public class LoginManagerImpl extends ActionSupport implements LoginManager,SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4999933159281378222L;
	
	private User user;
	//private LoginService loginService;
	private LoginDao loginDao;
	

	private Map<String, Object> session;
	
	/*@Before
	public void before(){
		System.out.println("before");
	}*/

	
	@Validations(requiredStrings={@RequiredStringValidator(fieldName="user.username",message="Username is required."),
			@RequiredStringValidator(fieldName="user.password",message="Password is required")})
	@Action(value="login",results={
			@Result(name="success",type="redirectAction",params={"actionName","list_movie.action","namespace","/"}),
			@Result(name="input",location="login/login.jsp")})
	public String execute(){
		
		session.clear();
		
		try{
			//user = loginService.authenticate(user.getUsername(), user.getPassword());
			System.out.println("executing");
			user = loginDao.getUser(user) ;
			
			if(user.getId() != 0){
				session.put("loginUser", user);				
				return SUCCESS;
			}
			
			
		}catch(Exception e){
			System.out.println(e.getMessage());
		}
		return INPUT;
	}
	
	/*@BeforeResult
	public void beforeResult(){
		
	}*/
	
	/*@After
	public void after(){
		System.out.println("after");
	}*/

	
	public User getUser() {
		return user;
	}
	
	
	public void setUser(User user) {
		this.user = user;
	}

	/*public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}*/
	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}
	
	public void setSession(Map<String, Object> arg0) {
		session = arg0;
		
	}

	/*@Override
	public void validate() {
		if(user.getUsername() == null || user.getUsername().trim().equals("")){
			//System.out.println("user name is null");
			addActionMessage("User cannot be null");
		}
		
		if(user.getPassword() == null || user.getPassword().trim().equals("")){
			//System.out.println("password is null");
			addActionMessage("Password cannot be null");
		}
	}*/
	
}
